package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.exception.ApiException;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizWithdrawBankCard;
import cn.sfrog.project.biz.admin.domain.vo.BizWithdrawBankCardFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizWithdrawBankCardQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizWithdrawBankCardVO;
import cn.sfrog.project.biz.admin.mapper.BizWithdrawBankCardMapper;
import cn.sfrog.project.biz.admin.service.IBizWithdrawBankCardService;
import cn.sfrog.project.biz.web.mapstruct.AppUserBankCardConverter;
import cn.sfrog.project.biz.web.vo.account.AppUserBankCardFormVO;
import cn.sfrog.project.biz.web.vo.account.AppUserBankCardResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jfinal.kit.HttpKit;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 提现银行卡Service业务层处理
 */
@Service
public class BizWithdrawBankCardServiceImpl extends ServiceImpl<BizWithdrawBankCardMapper, BizWithdrawBankCard> implements IBizWithdrawBankCardService {
    @Resource
    private BizWithdrawBankCardMapper bizWithdrawBankCardMapper;

    /**
     * 查询提现银行卡
     *
     * @param id 提现银行卡主键
     * @return 提现银行卡
     */
    @Override
    public BizWithdrawBankCardVO selectBizWithdrawBankCardById(Long id) {
        return bizWithdrawBankCardMapper.selectBizWithdrawBankCardById(id);
    }

    /**
     * 查询提现银行卡列表
     *
     * @param query 提现银行卡
     * @return 提现银行卡
     */
    @Override
    public List<BizWithdrawBankCardVO> selectBizWithdrawBankCardList(BizWithdrawBankCardQueryVO query) {
        return bizWithdrawBankCardMapper.selectBizWithdrawBankCardList(query);
    }

    /**
     * 新增提现银行卡
     *
     * @param form 提现银行卡
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizWithdrawBankCard(BizWithdrawBankCardFormVO form) {
        BizWithdrawBankCard bizWithdrawBankCard = new BizWithdrawBankCard();
        BeanUtils.copyProperties(form, bizWithdrawBankCard);
        bizWithdrawBankCard.setCreateTime(new Date());
        bizWithdrawBankCard.setCreateBy(SecurityUtils.getUsername());
        bizWithdrawBankCard.setUpdateTime(new Date());
        bizWithdrawBankCard.setUpdateBy(SecurityUtils.getUsername());
        return bizWithdrawBankCard.insert();
    }

    /**
     * 修改提现银行卡
     *
     * @param form 提现银行卡
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizWithdrawBankCard(BizWithdrawBankCardFormVO form) {
        BizWithdrawBankCard bizWithdrawBankCard = new BizWithdrawBankCard();
        BeanUtils.copyProperties(form, bizWithdrawBankCard);
        bizWithdrawBankCard.setUpdateTime(new Date());
        bizWithdrawBankCard.setUpdateBy(SecurityUtils.getUsername());
        return bizWithdrawBankCard.updateById();
    }

    /**
     * 批量删除提现银行卡
     *
     * @param ids 需要删除的提现银行卡主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizWithdrawBankCardByIds(Long[] ids) {
            return BizWithdrawBankCard.DAO.delete(Wrappers.lambdaQuery(BizWithdrawBankCard.class).in(BizWithdrawBankCard::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean saveBankCard(AppUserBankCardFormVO form) {
//        boolean isCard = CheckBankCardUtils.checkBankCard(form.getBankCardNumber());
        Boolean aBoolean = checkBankCardByAli(form.getBankCardNumber());
        if(!aBoolean){
            throw new ApiException("银行卡格式有误,请重新填写");
        }
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        LambdaQueryWrapper<BizWithdrawBankCard> lambdaQueryWrapper = new LambdaQueryWrapper<BizWithdrawBankCard>()
                .eq(BizWithdrawBankCard::getBankCardNumber,form.getBankCardNumber())
                .eq(BizWithdrawBankCard::getUserId,userId);
        BizWithdrawBankCard withdrawBankCard = bizWithdrawBankCardMapper.selectOne(lambdaQueryWrapper);
        if(StringUtils.isNotNull(withdrawBankCard)){
            throw new ApiException("您已经添加过该银行卡了");
        }
        BizWithdrawBankCard bankCard = AppUserBankCardConverter.INSTANCE.formTo(form);
        bankCard.setUserId(userId);
        return bankCard.insert();
    }

    @Override
    public List<AppUserBankCardResultVO> selectAppUserBankCardList(Long userId) {
        return bizWithdrawBankCardMapper.selectAppUserBankCardList(userId);
    }

    public  Boolean checkBankCardByAli(String bankCardNumber){
        String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo="+bankCardNumber+"&cardBinCheck=true";
        String s = HttpKit.get(url);
        JSONObject jsonObject = JSONObject.parseObject(s);
        return  jsonObject.getBoolean("validated");
    }

//    public static void main(String[] args) {
//        Boolean aBoolean = checkBankCardByAli("6222005865412565805");
//        System.out.println(aBoolean);
//    }
}
